﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ModuleDALCore;
using DTO;

namespace DAL
{
	public class TOAMAUTHUOCDAO:DALBase
	{
		private string _SpName;
		public string SpName
		{
			get { return "SPTOAMAUTHUOC"; }
			set { _SpName = value; }
		}

		public TOAMAUTHUOCDAO()
		{
			this.OpenConnect();
		}
		private DataTable dt;
		public void BEGINTRANSACTION()
		{
			DBManager.Intance.BeginTransaction();
		}
		public void CLOSE()
		{
			DBManager.Intance.CloseConnection();
		}
		public void COMMIT()
		{
			DBManager.Intance.CommitTransaction();
		}
		public void ROLLBACK()
		{
			DBManager.Intance.RollbackTransaction();
		}
		
		public DataTable GetListMT()
		{
			dt = new DataTable();
			try
			{
				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(new TOAMAUTHUOC(), null), ConvertClassToArray.GetValues(new TOAMAUTHUOC(), 0));
				return dt;
			}
			catch { return null; }
		}
		public List<TOAMAUTHUOC> GetListMTs()
		{
			dt = new DataTable();
			List<TOAMAUTHUOC> ds = new List<TOAMAUTHUOC>();
			try
			{
				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(new TOAMAUTHUOC(), null), ConvertClassToArray.GetValues(new TOAMAUTHUOC(), 0));
				ds = (List<TOAMAUTHUOC>)ConvertDatatableToList.ConvertTo<TOAMAUTHUOC>(dt);		
				return ds;
			}
			catch { return null; }
		}
		public String CreatenewId()
		{
			try
			{
				return "MT" + DBManager.Intance.ExecuteNonQuery(SpName, ConvertClassToArray.GetParamName(new TOAMAUTHUOC(), null), ConvertClassToArray.GetValues(new TOAMAUTHUOC(), 1));
			}
			catch { return null; }
		}
		public long INSERTTM(TOAMAUTHUOC tm)
		{
			try
			{
				return long.Parse(DBManager.Intance.ExecuteNonQueryWithTransaction(SpName, ConvertClassToArray.GetParamName(tm, null), ConvertClassToArray.GetValues(tm, 2)).ToString());

			}
			catch { return 0; }
		}
		public bool UPDATETM(TOAMAUTHUOC tm)
		{
			try
			{
				return (DBManager.Intance.ExecuteNonQueryWithTransaction(SpName, ConvertClassToArray.GetParamName(tm, null), ConvertClassToArray.GetValues(tm, 3)) == 1);

			}
			catch { return false; }
		}
		public bool DELETETM(TOAMAUTHUOC tm)
		{
			try
			{
				return (DBManager.Intance.ExecuteNonQueryWithTransaction(SpName, ConvertClassToArray.GetParamName(tm, null), ConvertClassToArray.GetValues(tm, 4)) == 1);

			}
			catch { return false; }
		}
		public bool INSERTCT(CT_TOAMAUTHUOC ct)
		{
			try
			{
				return (DBManager.Intance.ExecuteNonQueryWithTransaction("SPCT_TOAMAUTHUOC", ConvertClassToArray.GetParamName(ct, null), ConvertClassToArray.GetValues(ct, 2)) == 1);
			}
			catch { return false; }
		}
		public bool UPDATECT(CT_TOAMAUTHUOC ct)
		{
			try
			{
				return (DBManager.Intance.ExecuteNonQueryWithTransaction("SPCT_TOAMAUTHUOC", ConvertClassToArray.GetParamName(ct, null), ConvertClassToArray.GetValues(ct, 3)) == 1);
			}
			catch { return false; }
		}
		public bool DELETECT(CT_TOAMAUTHUOC ct)
		{
			try
			{
				return (DBManager.Intance.ExecuteNonQueryWithTransaction("SPCT_TOAMAUTHUOC", ConvertClassToArray.GetParamName(ct, null), ConvertClassToArray.GetValues(ct, 4)) == 1);
			}
			catch { return false; }
		}
		
		public bool INSERT(TOAMAUTHUOC tm, List<CT_TOAMAUTHUOC> ct)
		{
			bool kq = true;
			try
			{
				this.BEGINTRANSACTION();
				long ma = this.INSERTTM(tm);

				if (ma > 0)
				{
					if (ct.Count == 0)
					{
						kq = false;
					}
					foreach (CT_TOAMAUTHUOC mc in ct)
					{
						mc.IDTOATHUOC = ma;
						kq = INSERTCT(mc);
					}
				}
				else
				{
					kq = false;
				}
				if (kq)
				{
					this.COMMIT();
				}
				else
				{
					this.ROLLBACK();
				}
			}
			catch { kq = false; this.ROLLBACK(); }
			this.CLOSE();
			return kq;
		}
		public bool UPDATE(TOAMAUTHUOC tm, List<CT_TOAMAUTHUOC> ct, List<CT_TOAMAUTHUOC> del)
		{
			bool kq = true;
			try
			{
				this.BEGINTRANSACTION();
				kq = this.UPDATETM(tm);
				long id = tm.IDTOATHUOC;
				if (del.Count > 0)
				{
					foreach (CT_TOAMAUTHUOC ctd in del)
					{
						ctd.IDTOATHUOC = id;
						kq = DELETECT(ctd);
					}
				}
				if (ct.Count > 0)
				{
					foreach (CT_TOAMAUTHUOC mc in ct)
					{
						mc.IDTOATHUOC = id;
						kq = UPDATECT(mc);
					}
				}
				if (kq)
				{
					this.COMMIT();
				}
				else
				{
					this.ROLLBACK();
				}
			}
			catch { kq = false; this.ROLLBACK(); }
			this.CLOSE();
			return kq;
		}
		public bool DELETE(TOAMAUTHUOC tm, List<CT_TOAMAUTHUOC> lct)
		{
			bool kq = true;
			try
			{
				this.BEGINTRANSACTION();
				kq = DELETETM(tm);
				foreach (CT_TOAMAUTHUOC ct in lct)
				{
					kq = DELETECT(ct);
				}
				if (kq)
				{
					this.COMMIT();
				}
				else
				{
					this.ROLLBACK();
				}
			}
			catch { kq = false; this.ROLLBACK(); }
			this.CLOSE();
			return kq;
		}
		public TOAMAUTHUOC GetTOAMAU(TOAMAUTHUOC mv)
		{
			dt = new DataTable();
			TOAMAUTHUOC ds = new TOAMAUTHUOC();
			try
			{
				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(mv, null), ConvertClassToArray.GetValues(mv, 5));
				ds = (TOAMAUTHUOC)ConvertDatatableToList.ConvertToObject<TOAMAUTHUOC>(dt);
				return ds;
			}
			catch { return null; }
		}
		public List<TOAMAUTHUOC> Find(TOAMAUTHUOC tm)
		{
			dt = new DataTable();
			List<TOAMAUTHUOC> ds = new List<TOAMAUTHUOC>();
			try
			{
				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(tm, null), ConvertClassToArray.GetValues(tm, 6));
				ds = (List<TOAMAUTHUOC>)ConvertDatatableToList.ConvertTo<TOAMAUTHUOC>(dt);
				return ds;
			}
			catch { return null; }
		}

		public List<CT_TOAMAUTHUOC> GetListCTTMbyTMIDS(TOAMAUTHUOC tm)
		{
			dt = new DataTable();
			List<CT_TOAMAUTHUOC> ds = new List<CT_TOAMAUTHUOC>();
			try
			{
				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(tm, null), ConvertClassToArray.GetValues(tm, 7));
				ds = (List<CT_TOAMAUTHUOC>)ConvertDatatableToList.ConvertTo<CT_TOAMAUTHUOC>(dt);
				return ds;
			}
			catch { return null; }
		}

		public DataTable GetListCTTMbyTMID(TOAMAUTHUOC tm)
		{
			dt = new DataTable();
			try
			{
				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(tm, null), ConvertClassToArray.GetValues(tm, 7));
				return dt;
			}
			catch { return null; }
		}
	}
}